set more off
cap log close
cap clear all
set maxvar 30000
set matsize 11000

** Relevant directories
global path "N:\MedicareClaims-P045601-BE\Work\"
global hrpath "N:\MedicareClaims-P045601-BE\Work\hosp_retro"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global tpath "N:\MedicareClaims-P045601-BE\Work\hosp_retro\stata_do"
global nwpath "N:\MedicareClaims-P045601-BE\Work\hosp_retro\Nathan\Data"

* log 
log using "$hrpath\Nathan\Logs\Merger Construction.txt", replace t 

********************************************************************************

* import new mergers
	import excel "N:\MedicareClaims-P045601-BE\Work\hosp_retro\MergerBase.xlsx", sheet("Trimmed") firstrow clear

* make sure that only 1 hosp involved per line	
	assert BuyerHN != . if TargetHN == .
	assert TargetHN!=. if BuyerHN == .

* reduce to one provider number per line
	gen prov = BuyerHN
	replace prov = TargetHN if prov == .
	keep if prov != .
	
* get system dummy
	assert TargetSy == . if BuyerS != .
	assert BuyerS == . if TargetS != .
	gen sysdum = (TargetSy != . | BuyerS != .)
	
* rename date variables to match original data
	rename Announced Announce_Date
	rename Closed Closing_Date
	
* get merger identifier	
	gen id = BuyerName + " - " + TargetName
	ren BuyerName Buyer
	ren TargetName Target
	
* limit to good data
	keep *Date id prov Buyer Target sysdum
	
* gen source of info
	gen source = "Levin"
	
* save to be appended
	tempfile temp
	save `temp', replace
	
*******************************************************************************

* get nice id in old data
	import excel "N:\MedicareClaims-P045601-BE\Work\hosp_retro\mergers_make.xlsx", sheet("All") firstrow clear
	keep N Target Buyer
	gen id = Buyer + " - " + Target	
	ren N n
	keep n id Target Buyer
	gen source = "Lexis"	

* merge to cleaned old data
	merge 1:m n using "$hrpath\merger_dates", 
	keep if _m == 3
	drop _m n
	
* clean up the Avanti stuff: codes for hospitals not part of system at time of merger
	local avanti 50771 50219 50091
	egen drop = anymatch(prov), values(`avanti')
	drop if drop == 1
	drop drop	

* get sysdum
	bys id: gen sysdum = (_N != 1)
	
******************************************************************************* 	

* append new data to old, and encode the id of the merger
	append using `temp'

* check that not doublecounting
	order source Announce Clos Last id prov
	sort Announce id
	* do by observation  -- > suggests that there was one (Ortho - Fort) which 
	* made it through. I addressed by removing new observation from imported 
	* Lewin dataset
	
********************************************************************************

/* restrict by dates. Keep if at least two quarters of pre and post. Use last 
recorded date as merger date */

	egen date = rowmax(Announce Clos)
	gen q_of_merger = qofd(date)
	format q_of_merger %tq
	
	gen drop = (q_of_merger < tq(2005q3) | q_of_merger > tq(2010q2))
	tab drop
	tab id if drop
	
	drop if drop == 1
	
	drop drop
	
/* deal with multiple mergers */
	bys prov: gen num_merg = _N	

/* gen heart/cardio merger dummy */
	gen cardio = regexm(id, "Heart") | regexm(id, "Cardio")	
	
/* gen repeater */ 
	egen tag = tag(id)
	bys Buyer: egen sumtag = sum(tag)
	gen repeater = (sumtag>1)
	drop tag sumtag
	
/* get num of affected hosp */
	bys id: gen num_hosp = _N

/* good ID variable */
	sort Announce Buyer Target
	egen tag = tag(id)
	gen ID = sum(tag)
	drop tag
	
	local N = _N
	forvalues n = 1/`N' {
		label define mergerid `=ID[`n']' "`=id[`n']'", modify
	}
	label values ID mergerid
	
	drop id	
	
/* assess the set */
	egen tag = tag(ID)
	bys source: egen sumtag = sum(tag)
	bys source: su sumtag
	drop sumtag tag
	
/* look at types of merger */
	tab ID sysdum
	tab ID cardio
	tab ID repeater
	
/* save */
	save "$nwpath\merger_info", replace
